<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="auth" uri="auth"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="${ctx}/css/common.css" />
<link rel="stylesheet" href="${ctx}/css/pagination.css" />

<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/jquery.pagination.js"></script>

<script language="javascript">
var pageIndex = 0;
var pageSize = 10;

$(document).ready(function() {
    //分页
    InitTable(0);
    
    $("#entityPage").pagination(${entityCount}, {
            callback: agentPageCallback,  
            prev_text: '<s:text name="PrePage" />',       //上一页按钮里text  
            next_text: '<s:text name="NextPage" />',       //下一页按钮里text  
            items_per_page: pageSize,  //显示条数  
            num_display_entries: 6,    //连续分页主体部分分页条目数  
            current_page: pageIndex,   //当前页索引  
            num_edge_entries: 2        //两侧首尾分页条目数  
    });
    
    //翻页调用  
    function agentPageCallback(index,jq) {
    	InitTable(index);
    }
    
    //初始化表格
    function InitTable(pageIndex) {
    	top.showProgress("<s:text name='LoadingEntityData' />....");
    	
    	if($("#entityName").val() == ""){
    		var ajaxUrl = "../ajax/getEntityByPage.action?page="+pageIndex+"&pageSize="+pageSize + "&t=" + Math.random();     //提交到一般处理程序请求数据
    	} else {
    		var ajaxUrl = "../ajax/getEntityByPage.action?page="+pageIndex+"&pageSize="+pageSize +
    				"&search=" + encodeURI(encodeURI($("#entityName").val())) + "&t=" + Math.random();     //提交到一般处理程序请求数据
    	}
	
    	$("#entityList tbody").html("");
        
        $.getJSON(ajaxUrl,function(data){
        	html = "";

        	if(data.length == 0){
        		html += "<tr>\r\n"+
							"<td height='40' colspan='6' class='td_content'><s:text name='HasNoEntityData' /></td>\r\n"+
						"</tr>\r\n";
        	} else {
        		$.each(data,function(id,value){
                	html += "<tr onmouseover=\"this.style.background='#F2F2F2'\" onmouseout=\"this.style.background='#EEF7FD'\" onclick=\"changeChildShow("+value.id+");\">\r\n"+
		                	"<td class=\"td_content\">"+value.id+"</td>"+
		                    "<td class=\"td_content\">"+value.appVo.appName+"</td>"+
		                    "<td class=\"td_content\"><img id=\"allowImg_"+value.id+"\" src=\"${ctx}/images/app/allow_down.png\" />"+value.entityName+"</td>"+
		                    "<td class=\"td_content\">"+value.entityTypeVo.entityTypeName+"</td>"+
		                    "<td class=\"td_content\">"+value.entityCode+"</td>";
		                    
		            
        			html += "<td class='td_content'>"+
        					"<auth:entity entityCode='UPDATE_ENTITY'><a href='javascript:updateEntity("+value.id+")'><s:text name='Update' /></a> ｜ </auth:entity>"+
        					"<auth:entity entityCode='DEL_ENTITY'><a href='javascript:del("+value.id+");'><s:text name='Delete' /></a></auth:entity></td>\r\n"+
        			   		"</tr>\r\n";
        			
        			var pages = value.child;
        			
        			$.each(pages,function(id,page){
        				html += "<tr name=\"child_"+value.id+"\" onmouseover=\"this.style.background='#F2F2F2'\" onmouseout=\"this.style.background='#EEF7FD'\">\r\n"+
			                	"<td class=\"td_content\">"+page.id+"</td>"+
			                    "<td class=\"td_content\">"+page.appVo.appName+"</td>"+
			                    "<td class=\"td_content\">&nbsp;&nbsp;<img src=\"${ctx}/images/app/page.png\" />&nbsp;"+page.entityName+"</td>"+
			                    "<td class=\"td_content\">"+page.entityTypeVo.entityTypeName+"</td>"+
			                    "<td class=\"td_content\">"+page.entityCode+"</td>";
	                    
	            
    					html += "<td class='td_content'>"+
    							"<auth:entity entityCode='UPDATE_ENTITY'><a href='javascript:updateEntity("+page.id+")'><s:text name='Update' /></a> ｜ </auth:entity>"+
		    					"<auth:entity entityCode='DEL_ENTITY'><a href='javascript:del("+page.id+");'><s:text name='Delete' /></a></auth:entity></td>\r\n"+
		    			   		"</tr>\r\n";
		    			   		
    					var buttons = page.child;
    					
            			$.each(buttons,function(id,button){
            				html += "<tr name=\"child_"+value.id+"\" onmouseover=\"this.style.background='#F2F2F2'\" onmouseout=\"this.style.background='#EEF7FD'\">\r\n"+
    			                	"<td class=\"td_content\">"+button.id+"</td>"+
    			                    "<td class=\"td_content\">"+button.appVo.appName+"</td>"+
    			                    "<td class=\"td_content\">&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"${ctx}/images/app/button.png\" />&nbsp;"+button.entityName+"</td>"+
    			                    "<td class=\"td_content\">"+button.entityTypeVo.entityTypeName+"</td>"+
    			                    "<td class=\"td_content\">"+button.entityCode+"</td>";
    	                    
    	            
        					html += "<td class='td_content'>"+
        							"<auth:entity entityCode='UPDATE_ENTITY'><a href='javascript:updateEntity("+button.id+")'><s:text name='Update' /></a> ｜ </auth:entity>"+
    		    					"<auth:entity entityCode='DEL_ENTITY'><a href='javascript:del("+button.id+");'><s:text name='Delete' /></a></auth:entity></td>\r\n"+
    		    			   		"</tr>\r\n";
            			});
        			});
                });
        	}
        	
    		$("#entityList tbody").append(html);
    		
    		top.hideProgress();
        });                  
    }
});

function changeChildShow(id){
	if($("[name='child_"+id+"']").is(":hidden")){
		$("#allowImg_" + id).attr("src","${ctx}/images/app/allow_down.png");
	} else {
		$("#allowImg_" + id).attr("src","${ctx}/images/app/allow_right.png");
	}
	
	$("[name='child_"+id+"']").toggle();
}

function del(id){
	parent.ymPrompt.confirmInfo("<s:text name='ConfirmToDeleteThisEntity' />？",null,
			null,null,
			function(tp){
				if(tp=="ok"){
					window.location.href="delEntity.action?id="+id;
				}
			}
	);
}

function addEntity(){
	parent.ymPrompt.win('editEntityPage.action',500,300,'<s:text name="AddEntity" />',null,null,null,{id:'editEntityWin'});
}

function updateEntity(id) {
	parent.ymPrompt.win('editEntityPage.action?id='+id,500,300,'<s:text name="UpdateEntity" />',null,null,null,{id:'editEntityWin'});
}
</script>

</head>

<body>
	<div class="table_header">
		<span class="search_bar"><s:text name="SearchEntity" /></span>
	</div>
	<div class="table_content">
		<form action="searchEntity.action" method="post">
			<span style="padding-left:10px;"><s:text name="EntityName" /></span>：
			<input id="entityName" name="entityName" type="text" value="${entityName }" />
			<input type="submit" value="搜索" />
		</form>
	</div>
	
	<div class="clear">
	</div>
	
	<div class="table_header">
		<auth:entity entityCode="ADD_ENTITY">
    	<a href="javascript:addEntity();">
    		<span class="create_btn"><s:text name="AddEntity" /></span>
        </a>
        </auth:entity>
    </div>
	<table id="entityList" width="100%" cellspacing="1" cellpadding="5" class="table_class">
    	<thead>
        	<tr>
            	<td class="td_header">ID</td>
                <td class="td_header"><s:text name="AppName" /></td>
                <td class="td_header"><s:text name="EntityName" /></td>
                <td class="td_header"><s:text name="EntityType" /></td>
                <td class="td_header"><s:text name="EntityCode" /></td>
                <td class="td_header"><s:text name="Operate" /></td>
            </tr>
        </thead>
        <tbody>
        </tbody>
        <tfoot>
	        <tr>
				<td colspan="6" class="td_content">
					<div id="entityPage" class="pagination"></div>
				</td>
			</tr>
        </tfoot>
    </table>
</body>
</html>
